$(function() {
	//切换验证码
	$('#change').click(function() {
		$('#img').attr('src', 'v1/users/captcha?d' + Math.random())
	})
	//提交表单前验证
	$('#loginsubmit').click(function(event) {
		event.preventDefault()
		let username = $('#username').val();
		let password = $('#password').val();
		let type = $('#userType').val();
		let yzm = $('#yzm').val();
		let flag = true;
		let captcha = document.cookie.split('=');
		 let i =captcha.length-1;
		 captcha=captcha[i]
		if (username == "") {
			$('#name_trip').show();
			flag = false;
		} else if (password == "") {
			$('#password_trip').show();
			flag = false;
		} else if (type == "") {
			$("#type_trip").show();
			flag = false;
		} else if (yzm == "") {
			$('#yzm_trip').show();
			flag = false;
		} else if (yzm != "" && yzm.toUpperCase() != captcha.toUpperCase()) {//忽略大小写
			$('#yzm_error').show();
			$('#yzm_trip').hide();
			flag = false;
		} else {
			$.ajax({
				url: "v1/users/login",
				type: "post",
				async: false,
				data: {
					username: $("#username").val(),
					password: $("#password").val(),
					usertype: $("#userType").val(),
				},
				success: function(data) {
					//console.log(data.data)
					if (data.status == 200){
						alert('登陆成功');
						if(data.data==0){
							window.location = '/homePage';
						}else{
							window.location = '/teacher';
						}
						
					} else{
						alert('登陆失败,账号或密码错误');
					}
					
				},
				error: function(err) {
					console.log(err);
				},
			})
		}

		return flag;

	})
	
	
	
	// //重置
	// $('#reset').click(function(){
	// 	let username = $('#phone').val();
	// 	let password = $('#password').val();
	// 	let type = $('#userType').val();
	// 	let yzm = $('#yzm').val();
	// 	username=="";
	// 	password=="";
	// 	type=="";
	// 	yzm==""
	// })
	
	

	//输入框改变
	$('#username').change(function() {
		if($('#username').val()==""){
			$('#name_trip').show();
		}else{
			$('#name_trip').hide();
		}
		
	})

	$('#password').change(function() {
		if($('#password').val()==""){
			$('#password_trip').show();
		}else{
			$('#password_trip').hide();
		}
		
	})

	$('#userType').change(function() {
		if($('#userType').val()==""){
			$('#type_trip').show();
		}else{
			$('#type_trip').hide();
		}
		
	})

	$('#yzm').change(function() {
		if($('#yzm').val()==""){
			$('#yzm_trip').show();
		}else{
			$('#yzm_trip').hide();
		}
		
	})

})
